<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>demo</title>
</head>
<body>

<br>
<span id="copyBtn" data-clipboard-action="copy" data-clipboard-text="假装是微信号" oncopy="copySucceed(this)">假装是微信号</span>
<!--<button id="copyBtn" data-clipboard-action="copy" data-clipboard-target="#demo">点击复制</button>-->
<br>
<br>
<br>
<br>
<br>
<script type="text/javascript" src="js/clipboard.min.js"></script>
<script type="text/javascript" src="js/layer_mobile/layer.js"></script>
<script>
    var clipboard = new Clipboard('#copyBtn');

    clipboard.on('success', function (e) {
        selectText(e.trigger);
        copySucceed(e.trigger);
    });

    clipboard.on('error', function (e) {
        selectText(e.trigger);
        //提示
        layer.open({
            content: '请长按所选内容进行复制',
            skin: 'msg',
            time: 3 //秒后自动关闭
        });
    });

    function copySucceed(e) {
        //复制成功
        var innerTxt = e.innerText;//目标复制区域的文本
        var selectObj = window.getSelection() || document.getSelection();//用户选择的区域
        var txt = (selectObj.toString()).replace(/(^\s*)|(\s*$)/g, "");//用户选择的文本，去除前后空格
        //判断用户是否选择了目标复制区域的完整内容
        if (txt && txt == innerTxt) {
            //提示
            layer.open({
                content: '复制成功，即将打开微信',
                skin: 'msg',
                time: 2 //秒后自动关闭
            });
            setTimeout(function () {
                if (!document.getElementById('aWeiXinLink')) {
                    //向页面写入打开微信的a标签
                    var aWeiXinLink = document.createElement('a');
                    document.body.appendChild(aWeiXinLink);
                    aWeiXinLink.href = "weixin://";
                    aWeiXinLink.id = "aWeiXinLink";
                }
                document.getElementById('aWeiXinLink').click();//主动触发微信a标签链接，打开微信
            }, 1500);
        } else {
            //多选或少选
            //提示
            layer.open({
                content: '复制错误，请选择完整微信号进行复制',
                skin: 'msg',
                time: 2 //2秒后自动关闭
            });
        }
    }

    /**
     * 选择一个区域，并返回区域内文字
     * @param element
     * @returns {*}
     */
    function selectText(element) {
        var selectedText;

        if (element.nodeName === 'SELECT') {
            element.focus();

            selectedText = element.value;
        }
        else if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
            var isReadOnly = element.hasAttribute('readonly');

            if (!isReadOnly) {
                element.setAttribute('readonly', '');
            }

            element.select();
            element.setSelectionRange(0, element.value.length);

            if (!isReadOnly) {
                element.removeAttribute('readonly');
            }

            selectedText = element.value;
        }
        else {
            if (element.hasAttribute('contenteditable')) {
                element.focus();
            }

            var selection = window.getSelection();
            var range = document.createRange();

            range.selectNodeContents(element);
            selection.removeAllRanges();
            selection.addRange(range);

            selectedText = selection.toString();
        }
        return selectedText;
    }

</script>
</body>
</html>